﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TEAA;

namespace TEAA
{
    public partial class Data : System.Web.UI.Page
    {
        SystemManager sm = SystemManager.GetSystemManager();
        RecordManager recordManager = BinaryRecordManager.GetRecordManager();
        List<string> attributes = null;
        DataSet insertDataSet = new DataSet("InsertTable");
        DataSet viewDataSet = new DataSet("ViewTable");

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["page"] = "Data";
            if (IsPostBack)
            {
                CreateInsertTable();
            }
            else
            {
                ((SiteMaster)Master).fillddlDatabase();
                ((SiteMaster)Master).setddlDatabaseSelectedItem();
                fillddlTables();                
                CreateInsertTable();
                //fillViewDataTable();  //meglevo adatok visszatoltese
            }
        }

        protected void ddlTables_SelectedIndexChanged(object sender, EventArgs e)
        {                        
            string tableName = ddlTables.SelectedValue.ToString();
            attributes = sm.GetAttributes(tableName);                        
        }

        public void CreateInsertTable()
        {           
            TableRow trHeader = new TableRow();
            TableRow trContent = new TableRow();
            string table = ddlTables.SelectedValue.ToString();
            List<string> attributes = sm.GetAttributes(table);
            foreach (string item in attributes)
            {
                TableCell tch = new TableCell();
                TableCell tcc = new TableCell();

                Label l = new Label();
                l.Text = item;
                TextBox tb = new TextBox();
                tb.Width = 75;
                tb.ID = "txt" + item;

                tch.Controls.Add(l);
                tcc.Controls.Add(tb);

                trHeader.Controls.Add(tch);
                trContent.Controls.Add(tcc);
            }            

            insertTable.Rows.Add(trHeader);
            insertTable.Rows.Add(trContent);            
        }


        public void fillViewDataTable()
        {
            viewTable.Rows.Clear();
            viewDataSet = new DataSet("ViewTable");
            string table = ddlTables.SelectedValue.ToString();
            if (table == null) table = ddlTables.Items[0].ToString();
            viewDataSet = recordManager.getAll(table);
            DataTable dt = viewDataSet.Tables[0]; // kiveszem a DataSetbol az elso DataTable-t            
            // EZT MAJD AKKOR FOGJUK HASZNALNI, MIKOR LESZNEK MAR ADATAINK ES A METODUSNAK SE KELL PARAMETEREZNI
            
            List<string> attributes = sm.GetAttributes(table);

            // table header
            TableRow trHeader = new TableRow();            
            foreach (string item in attributes)
            { 
                TableCell tch = new TableCell();

                Label l = new Label();
                l.Text = item;              

                tch.Controls.Add(l);                

                trHeader.Controls.Add(tch);                
            }
            viewTable.Rows.Add(trHeader);
            
            // table content
            int rows = dt.Rows.Count;
            int cols = dt.Columns.Count;
            for (int i = 0; i < rows; i++)
            {
                TableRow row = new TableRow();                
                for (int j = 1; j < cols; j++)
                {
                    TableCell cell = new TableCell();
                    TextBox tb = new TextBox();
                    
                    tb.ID = "tb" + i + "_" + j;
                    tb.Width = 75;
                    tb.Text = dt.Rows[i][j].ToString();                    
                    cell.Controls.Add(tb);                    
                    row.Cells.Add(cell);
                }
                TableCell tc = new TableCell();
                CheckBox cb = new CheckBox();
                cb.ID = "cb" + i;
                tc.Controls.Add(cb);
                row.Cells.Add(tc);
                viewTable.Rows.Add(row);
            }
        }               

        protected void btnInsertData_Click(object sender, EventArgs e)
        {
            //AK-          
            string table = ddlTables.SelectedValue.ToString();
            if (table == null) table = ddlTables.Items[0].ToString();

            try
            {
            insertDataSet = new DataSet("InsertTable");
            DataTable insertDataTable = new DataTable(table);
            insertDataTable.Columns.Add(BinaryRecordManager.RECORDID_FIELD_NAME, typeof(RecordID));
            foreach (string attribute in sm.GetAttributes(table))
            {
                insertDataTable.Columns.Add(attribute, DataTypeUtils.GetType(sm.GetAttributeDataType(table, attribute)));
            }

            DataRow dr = insertDataTable.NewRow();
            List<string> attributes = sm.GetAttributes(table);
            
                foreach (string item in attributes)
                {
                    if (Request.Form["txt" + item] != string.Empty)
                    {
                        Control c1 = this.Master.FindControl("MainContent");
                        Control c2 = c1.FindControl("insertTable");
                        TextBox c3 = c2.FindControl("txt" + item) as TextBox;
                        dr[item] = c3.Text;
                    }
                }            
                insertDataTable.Rows.Add(dr);
                insertDataSet.Tables.Add(insertDataTable);
                
                recordManager.insert(insertDataSet);
               
                //AK+   
                fillViewDataTable();
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Data is not in corect type!');</script>");
                alertBox(ex);                
            }   
        }

        protected void btnUpdateData_Click(object sender, EventArgs e)
        {
            //fillViewDataTable();
            string table = ddlTables.SelectedValue.ToString();
            if (table == null) table = ddlTables.Items[0].ToString();
            viewDataSet = recordManager.getAll(table);
            if (viewDataSet.Tables[0].Rows.Count > 0)
            {
                int rows = viewDataSet.Tables[0].Rows.Count;
                int cols = viewDataSet.Tables[0].Columns.Count;                

                for (int i = 0; i < rows; i++)
                {
                    for (int j = 1; j < cols; j++)
                    {
                        Control c1 = this.Master.FindControl("MainContent");
                        Control c2 = c1.FindControl("viewTable");
                        TextBox c3 = c2.FindControl("tb" + i + "_" + j) as TextBox;
                        string cellValue = c3.Text;
                        viewDataSet.Tables[0].Rows[i][j] = cellValue;
                    }
                }
                recordManager.modify(viewDataSet);
                fillViewDataTable();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string table = ddlTables.SelectedValue.ToString();
            if (table == null) table = ddlTables.Items[0].ToString();
            viewDataSet = recordManager.getAll(table);
            if (viewDataSet.Tables[0].Rows.Count > 0)
            {
                int rows = viewDataSet.Tables[0].Rows.Count;
                int cols = viewDataSet.Tables[0].Columns.Count;
                List<RecordID> recodId = new List<RecordID>();

                for (int i = 0; i < rows; i++)
                {
                     Control c1 = this.Master.FindControl("MainContent");
                        Control c2 = c1.FindControl("viewTable");
                        CheckBox c3 = c2.FindControl("cb" + i) as CheckBox;
                        bool cellValue = c3.Checked ;
                        if (cellValue == true)
                        {
                            recodId.Add((RecordID)viewDataSet.Tables[0].Rows[i][0]);
                        }
                                            
                }

                foreach (RecordID item in recodId)
                {
                    recordManager.delete(table,item);
                }
                fillViewDataTable();
            }
        } 

        public void fillddlTables()
        {            
            try
            {
                List<string> tables = sm.GetTables();
                ddlTables.Items.Clear();
                foreach (string item in tables)
                {
                    ddlTables.Items.Add(new ListItem(item.ToString()));
                }                               

            }
            catch (Exception ex)
            {                
                alertBox(ex);
            }
        }

        private void alertBox(Exception ex)
        {
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
            string errorMessage = "";
            Exception exception = ex;
            while (exception != null)
            {
                errorMessage += exception.Message + "\\n";
                exception = exception.InnerException;
            }
            Response.Write("alert('" + errorMessage + "');");
            Response.Write("</script>");
        }

                     
    }
}